package com.sss.otep.web.actions.page.json;

import java.io.PrintWriter;
import java.io.StringWriter;

import com.opensymphony.xwork2.ActionSupport;
import com.sss.otep.client.grid.GridDataModel;
import com.sss.otep.service.LoadParameter;

public abstract class GridBaseAction<T> extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 904228337107279437L;
	private static final String JSON = "JSON";
	/**
	 * grid result
	 */
	
	private boolean success = true;
	private String errorMessage = "";
	private String stacktrace = "";
	
	private GridDataModel<T> gridModel;

	private LoadParameter param = new LoadParameter();

	
	public GridDataModel<T>  getGridModel() {
		return gridModel;
	}

	public void setGridModel(GridDataModel<T> gridModel) {
		this.gridModel = gridModel;
	}
	
	public String getJSON() {
		return execute();
	}

	public void setParam(LoadParameter param) {
		this.param = param;
	}

	public LoadParameter getParam() {
		return param;
	}

	@Override
	public String execute() {
		doProcess();
		
		return JSON;
	}

	public static String getStackTrace(Throwable t)
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        t.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }
	public abstract void doProcess();

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setErrorMessage(String errorMessage, Throwable t) {
		this.errorMessage = errorMessage;
		this.setStacktrace(t);
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setStacktrace(String stacktrace) {
		this.stacktrace = stacktrace;
	}
	
	public void setStacktrace(Throwable t) {
		this.stacktrace = getStackTrace(t);
	}

	public String getStacktrace() {
		return stacktrace;
	}

}
